#include "timer.h"

TIM_HandleTypeDef timer_handle = {0};
uint8_t timer_trigger_flag = 0;

void timer_init(uint16_t arr, uint16_t psc)
{
    timer_handle.Instance = TIM3;
    timer_handle.Init.Prescaler = psc;
    timer_handle.Init.Period = arr;
    timer_handle.Init.CounterMode = TIM_COUNTERMODE_UP;
    timer_handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
    
    HAL_TIM_Base_Init(&timer_handle);
    HAL_TIM_Base_Start_IT(&timer_handle);
}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
{
    if (htim->Instance != TIM3)
        return ;
    
    __HAL_RCC_TIM3_CLK_ENABLE();
    HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2);
    HAL_NVIC_SetPriority(TIM3_IRQn, 2, 1);
    HAL_NVIC_EnableIRQ(TIM3_IRQn);
}


void TIM3_IRQHandler(void)
{

    HAL_TIM_IRQHandler(&timer_handle);
}



void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if (htim->Instance != TIM3)
        return ;
    
    timer_trigger_flag = 1;
}

uint8_t timer_trigger_flag_get(void)
{
    uint8_t temp = timer_trigger_flag;
    timer_trigger_flag = 0;
    
    return temp;
}



